//
//  StringExtension.swift
//  MyTest
//
//  Created by guanzhongwang on 2021/4/10.
//

import Foundation
import UIKit
// 富文本设置
extension String{
    var asNSString: NSString {
        
        return self as NSString
    }
    func textNodeAttr(color:UIColor,font:UIFont,lineSpace:CGFloat=1)->NSMutableAttributedString{
        let attr = NSMutableAttributedString(string: self)
        let paragraphStyle = NSMutableParagraphStyle()
        let range = self.asNSString.range(of: self)
        paragraphStyle.lineSpacing = lineSpace
        attr.addAttributes([
            NSAttributedString.Key.font: font,
            NSAttributedString.Key.foregroundColor:color
            ], range: range)
        attr.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, NSString(string: self).length))
        return attr
    }
    /**
      sub，subFont，subColor， 要修改的文字、大小、颜色
     */
    func getAttributedString(_ sub:String,subColor:UIColor,subFont:UIFont,lineSpace:CGFloat=1)->NSMutableAttributedString{
        let attr = NSMutableAttributedString(string: self)
        let range = self.asNSString.range(of: sub)
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = lineSpace
        attr.addAttributes([
            NSAttributedString.Key.font: subFont,
            NSAttributedString.Key.foregroundColor:subColor
            ], range: range)
        attr.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, NSString(string: self).length))
        return attr
    }
    /**
     subs，subFonts，subColors， 要修改的文字、大小、颜色
     */
    func getAttributedString(_ subs:[String],subColors:[UIColor],subFonts:[UIFont],lineSpace:CGFloat=4)->NSMutableAttributedString{
        let attr = NSMutableAttributedString(string: self)
        var i = 0
        for s in subs{
            let range = self.asNSString.range(of: s)
            attr.addAttributes([
                NSAttributedString.Key.font: subFonts[i],
                NSAttributedString.Key.foregroundColor:subColors[i]
                ], range: range)
            i = i+1
        }
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = lineSpace
        attr.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, NSString(string: self).length))
        return attr
    }
    /**
     设置行间距
     */
    func getAttributedString(lineSpace:CGFloat)->NSMutableAttributedString{
        let attributedString = NSMutableAttributedString(string:self)
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = lineSpace
        attributedString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, NSString(string: self).length))
        return attributedString
    }
    
    static func ClassFromString(_ name:String)->UIViewController?{
        //1:动态获取命名空间
        guard let spaceName = Bundle.main.infoDictionary!["CFBundleExecutable"] as? String else {
            print("获取命名空间失败")
            return nil
        }
        let vcClass: AnyClass? = NSClassFromString(spaceName + "." + name) //VCName:表示试图控制器的类名
         // Swift中如果想通过一个Class来创建一个对象, 必须告诉系统这个Class的确切类型
        guard let typeClass = vcClass as? UIViewController.Type else {
             print("vcClass不能当做UIViewController")
             return nil
         }
        let vc = typeClass.init()
        return vc 
    }
    
}
